package ru.papergames.view.components.field
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.Timer;
	import mx.events.PropertyChangeEvent;
	import ru.papergames.model.vo.BallVO;
	
	
	

	public class BallClip extends Sprite
	{
		private static const COLORS:Array = [0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF,
											0xFF8888, 0x88FFFF, 0xFF88FF, 0xFFFF88, 0x8888FF, 0xFF8888, 0x88FF88];
											
		private static const BALL_SIZE:uint = 10;
		
		public static function getColorById(colorId:uint):uint {
			
			if (colorId >= COLORS.length) return 0xFFFFFF;
			
			return COLORS[colorId];		
		}
		
		
				
		private var _action_end_timer:Timer = new Timer(1000, 1);		
		
		private var animation_queue:Array = [];
		
				
		//data
		private var _data:BallVO;		
		public function get data():BallVO {
			return _data;
		}
		
		public function set data(val:BallVO):void {
			if (_data != val) {
				if (_data) {
					_data.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onBallPropertyChange);
				}
				
				_data = val;
				
				_data.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onBallPropertyChange);
				this.invalidateProperties();
			}
		}		
		
		public var isMine:Boolean = false;
		
		
		public function BallClip(data:BallVO = null)
		{
			super();	
			
			//this.filters = [new DropShadowFilter()];
			
			this.mouseEnabled = false;
			this.mouseChildren = false;
			
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
															
			_action_end_timer.addEventListener(TimerEvent.TIMER, onActionEnd);			
			
			this.data = data;
		}
		
		private function addedToStage(evt:Event):void {
			invalidateProperties();
		}
				
		public function get x_pos():uint {
			return _data.position.x;
		}
		
		public function set x_pos(val:uint):void {
			_data.position.x = val;
		}
		
		public function get y_pos():uint {
			return _data.position.y;
		}
		
		public function set y_pos(val:uint):void {
			_data.position.y = val;
		}
		
				
		public function get inAnimation():Boolean {
			return false;
		}
		
		
		//=============================================
				
		private function onBallPropertyChange(evt:PropertyChangeEvent):void {
			this.invalidateProperties();
		}
				
		private function drawBall():void {					
			var g:Graphics = this.graphics;
			
			g.clear();
			g.lineStyle(1, 0);
			g.beginFill( getColorById(data.coloreId) );
				g.drawCircle(0, 0, BALL_SIZE);
			g.endFill();
			
			//add color number at top of the ball
			var tf:TextField = new TextField();
			tf.selectable = false;
			
			var tFormat:TextFormat = new TextFormat();
			tFormat.color = 0;
			tFormat.size = 11;
			//tFormat.bold = true;
			tFormat.font = 'Tahoma';
			tFormat.align = TextFormatAlign.CENTER;
									
			tf.defaultTextFormat = tFormat;
			
			tf.text = data.coloreId.toString();	
			tf.width = 2*BALL_SIZE;
			
			tf.x = -BALL_SIZE;
			tf.y = -BALL_SIZE;
			
			this.addChild(tf);
			
		}
		
		public function invalidateProperties():void {			
			//draw
			drawBall();
			
			//positionate
			if (parent) {
				(parent as ActorsHolder).positionateActor(this, data.position);
			}
		}
		
		
		public function animate(action:String, duration:uint = 800):void {
			
			if (duration > 0) _action_end_timer.start();
			
		}
				
		protected function onActionEnd(evt:TimerEvent):void {
			_action_end_timer.stop();			
		}
		
		
		public function destroy():void {
			if (this.parent) this.parent.removeChild(this);
			
			
			if (_data) {
				_data.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onBallPropertyChange);
			}
			
			this._data = null;
			
			_action_end_timer.removeEventListener(TimerEvent.TIMER, onActionEnd);
			_action_end_timer = null;
						
		}
		
	}
}